package com.zhangsun.system.controller;

import com.zhangsun.base.bean.ResponseData;
import com.zhangsun.common.utils.ResponseUtil;
import com.zhangsun.system.entity.ResourceEntity;
import com.zhangsun.system.service.ResourceService;
import com.zhangsun.system.vo.resource.ResourceEditVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 获取所有资源
     *
     * @return 资源
     */
    @GetMapping("/tree")
    public ResponseData findResourceTree() {
        List<ResourceEntity> tree = resourceService.findResourceTree();
        return ResponseUtil.success(tree);
    }

    /**
     * 获取所有菜单
     *
     * @return 菜单
     */
    @GetMapping("/menu")
    @RequiresPermissions("system:resource:view")
    public ResponseData findMenuTree() {
        List<ResourceEntity> tree = resourceService.findMenuTree();
        return ResponseUtil.success(tree);
    }

    /**
     * 添加资源
     *
     * @param vo 资源
     */
    @PostMapping("/create")
    @RequiresPermissions("system:resource:edit")
    public ResponseData create(@Validated ResourceEditVo vo) {
        resourceService.create(vo);
        return ResponseUtil.success();
    }

    /**
     * 获取资源详情
     *
     * @param id 资源id
     * @return 详细
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:resource:view")
    public ResponseData findById(@NotBlank @PathVariable String id) {
        ResourceEntity resource = resourceService.findById(id);
        return ResponseUtil.success(resource);
    }

    /**
     * 更新资源
     *
     * @param vo 资源
     */
    @PutMapping("/update/{id}")
    @RequiresPermissions("system:resource:edit")
    public ResponseData update(@NotBlank @PathVariable String id, @Validated ResourceEditVo vo) {
        resourceService.update(id, vo);
        return ResponseUtil.success();
    }

    /**
     * 删除资源
     *
     * @param id 资源id
     * @return 结果
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("system:resource:delete")
    public ResponseData delete(@NotBlank @PathVariable String id) {
        resourceService.delete(id);
        return ResponseUtil.success();
    }
}
